/* 列表 */
var utils = require("../../utils/utils.js");
var config = require("../../config/config.js");
var api = require("../../api/api.js");
var storageManage = require("../../manage/storageManage.js");
var maxLength;
function init() {
  var that = this;
  //判断是否大于限制 .这里的判断是因为发表说说带入到值

  
  //删除图片
  that.removeImg = function (event) {
    // this.setData({ lock: true });
    let index = event.target.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否删除图片',
      success: function (res) {
        if (res.confirm) {
          that.data.imglist.splice(index, 1);
          that.setData({
            imglist: that.data.imglist
          })
          that.isAddBtnShowFun(that.data.imglist.length);
        }

      }
    })
  },

  //图片点击预览
  that.imgMagnifyShow = function (e) {
    // if (this.data.lock) {
    //   return
    // }
    utils.imgMagnifyShow(e, this.data.imglist)
  },
  //上传图片
  that.upload = function (e) {
    var num = e.currentTarget.dataset.max_num; //最多上传几张图片
    maxLength = that.data.maxLength ? that.data.maxLength : 6
    var pics = that.data.imglist;
    wx.chooseImage({
      count: maxLength- pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showToast({
          title: '图片上传中',
          icon:'none'
        })
        var imgsrc = res.tempFilePaths;
        utils.uploadimg({
          url: config.serverUrl + "/photo",
          path: imgsrc//选取的图片的地址数组
        }, function (data) {
          //新上传拼接到之前的数组里
          var imglist = that.data.imglist.concat(data.sucImgUrlArr);
          that.setData({
            imglist: imglist
          });
          that.isAddBtnShowFun(that.data.imglist.length)//判断是否大于限制
          wx.hideLoading()
        });
      }
    })
  }

  //图片是否要隐藏点击标志
  that.isAddBtnShowFun = function (num) {
    let isAddBtnShow = true;
    if (num >= that.data.maxLength) {
      isAddBtnShow = false;
    } else {
      isAddBtnShow = true;
    }
    this.setData({
      isAddBtnShow: isAddBtnShow
    })
  }
  
  that.isAddBtnShowFun(that.data.imglist.length)
  
};

module.exports = {
  init: init
};